using FluentValidation;
using Zhaoxi.Zhihu.Core.AppUserAggregate.Entites;
using Zhaoxi.Zhihu.Core.AppUserAggregate.Specification;
using Zhaoxi.Zhihu.SharedKerel.Messaging;
using Zhaoxi.Zhihu.SharedKerel.Repositoy;
using Zhaoxi.Zhihu.SharedKerel.Result;
using Zhaoxi.Zhihu.UseCases.Common.Interface;

namespace Zhaoxi.Zhihu.UseCases.AppUsers.Commands;


public record DeleteFolloweeUserCommand(int FolloweeId) : ICommand<Result>;

public class DeleteFolloweeUserCommandValidator : AbstractValidator<DeleteFolloweeUserCommand>
{
    public DeleteFolloweeUserCommandValidator()
    {
        RuleFor(command=> command. FolloweeId)
            .GreaterThan(0);
    }
}


public class DeleteFolloweeUserCommandHandler(
    IRepository<AppUser> appusers,
    IUser user) : ICommandHandler<DeleteFolloweeUserCommand, Result>
{
    public async Task<Result> Handle(DeleteFolloweeUserCommand request, CancellationToken cancellationToken)
    {
        var spec = new FolloweeUserByIdSpec(user.Id !. Value, request.FolloweeId);
        var appuser = await appusers.GetSingleOrDefaultAsync(spec, cancellationToken);
        if (appuser == null) return Result.NotFound("用户不存在");
        appuser.RemoveFollowee(request.FolloweeId);
        await appusers. SaveChangesAsync(cancellationToken);
        return Result.Success();
    }
}
    